package com.karaoke.android.kege.vm

import android.databinding.ObservableArrayList
import android.databinding.ObservableBoolean
import android.databinding.ObservableInt
import android.databinding.ObservableList
import android.databinding.adapters.ViewBindingAdapter
import android.widget.GridView
import com.gturedi.views.StatefulLayout
import com.karaoke.android.data.LoadMoreEvent
import com.karaoke.android.data.NetworkTools
import com.karaoke.android.data.domain.QSong
import com.karaoke.android.data.domain.Song
import com.karaoke.android.data.onUI
import com.karaoke.android.data.repository.SongRepository
import com.karaoke.android.kege.BR
import com.karaoke.android.kege.KegeActivity
import com.karaoke.android.kege.R
import com.karaoke.android.kege.adapter.SwipeGridBindingAdapter
import com.karaoke.android.library.databind.ItemBinding
import com.karaoke.android.library.mvvm.FragmentViewModel
import com.trello.rxlifecycle.kotlin.bindToLifecycle
import org.simple.eventbus.EventBus
import org.simple.eventbus.Subscriber

/**
 * Created by allen on 2017/4/22.
 */

abstract class BaseSongViewModel : FragmentViewModel()
{

    init
    {
        EventBus.getDefault().register(this)
    }

    override fun onDestroy()
    {
        EventBus.getDefault().unregister(this)
        super.onDestroy()
    }

    val qSong = QSong().apply { size = 60 }
    protected var state: StatefulLayout? = null
    protected var canLoadMore = false
    val adapter = SwipeGridBindingAdapter<Song>()
    val loading = ObservableBoolean()
    val songs: ObservableList<Song> = ObservableArrayList<Song>()
    //    val songs: List<Song> = ArrayList()
    val songBindings: ItemBinding<Song> = ItemBinding.of<Song>(BR.viewModel, R.layout.song_item)
    abstract val emptyString: String?
    abstract val errorString: String?

    val position = ObservableInt()

    val onViewAttachedToWindow = ViewBindingAdapter.OnViewAttachedToWindow {
        if (it.id == R.id.stateful)
            state = it as StatefulLayout?
    }

    fun onLoad()
    {
        if (songs.isNotEmpty()) return
        state?.showLoading("加载中...")
        SongRepository.getSongs(qSong).bindToLifecycle(this)
                .onUI({
                          canLoadMore = it.hasNextPage
                          if (it.list.isEmpty()) state?.showEmpty(emptyString) else state?.showContent(); songs.addAll(it.list)
                      }, {
                          it.printStackTrace()
                          state?.showError("加载失败", { onLoad() })
                      })
    }

    @Subscriber
    fun onLoadMore(e: LoadMoreEvent)
    {
        if (!loading.get() && canLoadMore)
        {
            loading.set(true)
            qSong.page += 1
            SongRepository.getSongs(qSong).bindToLifecycle(this)
                    .onUI({ canLoadMore = it.hasNextPage;songs.addAll(it.list);loading.set(false) }, { loading.set(false) })
        }
    }

    override fun onDestroyView()
    {
        super.onDestroyView()
        val v = GridView(KegeActivity.activity)
        v.firstVisiblePosition
    }
}
